package at.fhooe.stefanirndorfer.input;

import java.util.ArrayList;

import org.lwjgl.input.Mouse;

public class InputManager {
ArrayList<KeyInputListener> kil;
ArrayList<MouseInputListener> mil;

int wHeight;
int wWidth;
public int x;
public int y;
public int dx;
public int dy;


public InputManager(int windowWidth, int windowHeight){
	wHeight = windowHeight;
	wWidth = windowWidth;
	kil = new ArrayList<KeyInputListener>();
	mil = new ArrayList<MouseInputListener>();
}

public synchronized void  addKeyListener(KeyInputListener listener){
	kil.add(listener);
}

public synchronized void addMouseListener(MouseInputListener mouselistener){
	mil.add(mouselistener);
}

	public void removeKeyListener(KeyInputListener inputListener) {
		//TODO
	}

	public synchronized void removeMouseListener(MouseInputListener listener) {
		for(int i = 0; i < mil.size(); i++){
			if(mil.get(i).equals(listener)){
				mil.remove(i);
				break;
			}
		}
	}
	
	
	public synchronized void dispatchKeyEvents(int eventKey, boolean keyDown) {
		for(KeyInputListener inpLis: kil){
			inpLis.processKeyInput(eventKey, keyDown);
		}
	}

	public synchronized void dispatchMouseEvents() {
		x = Mouse.getX()-wWidth/2;
		y = (wHeight - Mouse.getY())-wHeight/2;
		dx = Mouse.getDX();
		dy = Mouse.getDY();
		for(MouseInputListener inpLis: mil){
			inpLis.processMouseInput(x, y, dx,dy);
		}
	}

	public synchronized void removeAll() {
		
		for (int i = kil.size()-1; i >= 0; i--) {
			kil.remove(i);
		}
		for (int i = mil.size()-1; i >= 0; i--) {
			mil.remove(i);
		}
		
	}

	
}
